package com.example.teacher.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teacher.entity.EduSubject;
import com.example.teacher.entity.excel.SubjectData;
import com.example.teacher.entity.subject.OneSubject;
import com.example.teacher.entity.subject.TwoSubject;
import com.example.teacher.listener.SubjectListener;
import com.example.teacher.mapper.EduSubjectMapper;
import com.example.teacher.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-05-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneSubject() {
        //查询出一级分类
        QueryWrapper<EduSubject> wrapper =new QueryWrapper<>();
        wrapper.eq("parent_id",0);
//        baseMapper在继承的serviceImpl中封装好了，在此可以直接用
        //又或者使用this.list(wrapper)
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapper);

        //查询二级分类
        QueryWrapper<EduSubject> wrapperTwo =new QueryWrapper<>();
        wrapperTwo.ne("parent_id",0);
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建返回的对象
        List<OneSubject> finaSubjectList =new ArrayList<>();

        //封装一级分类
        for (EduSubject eduSubject : oneSubjectList) {
            OneSubject oneSubject=new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //等同上面两句 第一个参数是get对象，第二个参数是set对象
            BeanUtils.copyProperties(eduSubject,oneSubject);
//          拿到当前一级分类对象的id
            String id = oneSubject.getId();
            List<TwoSubject> list=new ArrayList<>();
            //封装二级分类
            for (EduSubject eduSubject2 : twoSubjectList) {
                if (eduSubject2.getParentId().equals(id)) {//二级分类的父类是否是当前对象
                    //如果是则封装，
                    TwoSubject twoSubject =new TwoSubject();
//                  twoSubject.setId(eduSubject.getId());
//                  twoSubject.setTitle(eduSubject.getTitle());
                    //等同上面两句
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    list.add(twoSubject);
                }
            }
            oneSubject.setChildren(list);
            finaSubjectList.add(oneSubject);
        }

        return finaSubjectList;
    }
}
